package com.bjfu.face.controller;

import com.baidu.aip.util.Base64Util;
import com.bjfu.common_util.R;
import com.bjfu.face.service.FaceService;
import com.bjfu.face.utils.BaiduUtils;
import com.google.gson.Gson;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/faceservice")
@CrossOrigin
public class FaceController {
    @Autowired
    private FaceService faceService;

    @Autowired
    private BaiduUtils baiduUtils;

    @PostMapping(value = "face/detect")
    @ApiOperation("人脸检测")
    public R faceDetect(@ApiParam(name = "file",value = "上传文件要写为@RequestPart") @RequestPart(name = "file") MultipartFile image) throws Exception {
        String encodeImage = Base64Util.encode(image.getBytes());
        JSONArray res = baiduUtils.faceDetect(encodeImage);
        String s = res.toString(2);
        Gson gson = new Gson();
        List list = gson.fromJson(s, List.class);
        return R.ok().data("detect",list);
    }
}
